package cn.mygweb.admin.visit.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.mygweb.admin.modules.sys.pojo.User;
import cn.mygweb.common.controller.BaseController;
import cn.mygweb.common.utils.IPUtils;
import cn.mygweb.common.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;

/** 
* <p>Title: AdminIndexController.java</p>
* <p>Description: 后台登录退出</p>  
* @author gxb 
* @date 2019年5月25日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/ 
@Controller
@RequestMapping("/admin")
@Slf4j
public class LoginController extends BaseController{
	
	/**
	 * <p>Title: toLogin</p>
	 * <p>Description: 访问login页面</p>
	 * @param user
	 * @return
	 */
	@RequestMapping("/toLogin")
	public String toLogin(User user) {
		Subject subject = SecurityUtils.getSubject();
		User u = (User) subject.getPrincipal();
		if(null == u) {
			return "admin/visit/login";
		} else {
			return "redirect:/admin/index";
		}
	}
	
	/**
	 * <p>Title: login</p>
	 * <p>Description: 后台登录页面</p>
	 * @return
	 */
	@RequestMapping("/login")
	public String login(User user, boolean rememberMe, ModelMap model) {
		//shiro认证操作
		//1.获取Subject
		Subject subject = SecurityUtils.getSubject();
		//2.封装用户数据，并判断记住我功能
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword(), rememberMe);
		//3.处理登录请求
		try {
			//登录成功
			subject.login(token);
			Session session = subject.getSession();
			session.setTimeout(1000*60*30);//设置session过期时长为30分钟，单位ms
			
			//更新用户登录时间、登录IP
			User newUser = userService.findByUsername(user.getUsername());
			newUser.setLastOnline(new Date());
			//String ip = session.getHost();
			String ip = IPUtils.getIpAddr();
			newUser.setLastLoginIp(ip);
			userService.updateUser(newUser);
			log.info("用户【 " + user.getUsername() + " 】请求登录成功，登录ip：" + ip + "时间：" + TimeUtils.currentTime());
			
			return "redirect:/admin/index";
		} catch (UnknownAccountException e) {
			//未知用户名
			model.addAttribute("unknownAccount", "用户名不存在");
			log.info("用户【 " + user.getUsername() + " 】请求登录，但是该用户不存在，时间：" + TimeUtils.currentTime());
			return "admin/visit/login";
		} catch (IncorrectCredentialsException e) {
			//密码错误
			model.addAttribute("incorrectCredential", "密码错误");
			log.info("用户【 " + user.getUsername() + " 】请求登录，但是密码错误，时间：" + TimeUtils.currentTime());
			return "admin/visit/login";
		}
	}
	
	/**
	 * <p>Title: logout</p>
	 * <p>Description: 退出后台系统</p>
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		Subject subject = SecurityUtils.getSubject();
//		User user = (User)subject.getPrincipal();
//		user.setLastOnline(new Date());
//		userService.updateUser(user);
		subject.logout();
		return "redirect:/admin/toLogin";
	}
}
